﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace RexToy.Collections
{
    public class ReadOnlyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>
    {
        private Dictionary<TKey, TValue> _dict;
        internal ReadOnlyDictionary(Dictionary<TKey, TValue> dict)
        {
            Assertion.IsNotNull(dict, "dict is null.");
            _dict = dict;
        }

        #region IReadOnlyDictionary<TKey,TValue> Members

        public IEnumerable<TKey> Keys
        {
            get { return _dict.Keys; }
        }

        public IEnumerable<TValue> Values
        {
            get { return _dict.Values; }
        }

        public TValue this[TKey key]
        {
            get { return _dict[key]; }
        }

        public bool ContainsKey(TKey key)
        {
            return _dict.ContainsKey(key);
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            return _dict.TryGetValue(key, out value);
        }

        public int Count
        {
            get { return _dict.Count; }
        }

        #endregion

        #region IEnumerable<KeyValuePair<TKey,TValue>> Members

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            return _dict.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _dict.GetEnumerator();
        }

        #endregion

        public static implicit operator ReadOnlyDictionary<TKey, TValue>(Dictionary<TKey, TValue> dict)
        {
            return new ReadOnlyDictionary<TKey, TValue>(dict);
        }

        public static explicit operator Dictionary<TKey, TValue>(ReadOnlyDictionary<TKey, TValue> dict)
        {
            return dict._dict;
        }
    }
}
